package com.jingtuo.android.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;

public class HttpUtils {
	public static final String USER_AGENT = "jingtuo";
	public static final int CONNECTION_TIMEOUT = 5*6000;
	public static final int READ_TIMEOUT = 5*6000;
	public static final int BUFFER_SIZE = 64 * 1024;
	
	public static String doPost(String url, HttpEntity entity){
		String result = null;
		AndroidHttpClient client = AndroidHttpClient.newInstance(USER_AGENT);
		client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
		client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, READ_TIMEOUT);
		client.getParams().setParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, BUFFER_SIZE);
		try {
			HttpPost request = new HttpPost(url);
			request.setEntity(entity);
			HttpResponse response = client.execute(request);
			if(response!=null&&response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
				String temp = null;
				StringBuilder stringBuilder = new StringBuilder();
				while(true){
					temp = br.readLine();
					if(temp!=null){
						stringBuilder.append(temp);
					}else{
						break;
					}
				}
				result = stringBuilder.toString();
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		client.close();
		return result;
	}
	
	/**
	 * 
	 * @param url
	 * @param data
	 */
	public static Bitmap getBitmap(String uri){
		AndroidHttpClient client = AndroidHttpClient.newInstance(USER_AGENT);
		client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
		client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, READ_TIMEOUT);
		client.getParams().setParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, BUFFER_SIZE);
		HttpGet httpGet = new HttpGet(uri);
		HttpResponse response = null;
		try {
			response = client.execute(httpGet);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Bitmap result = null;
		if(response!=null&&response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
			try {
				InputStream is = response.getEntity().getContent();
				result = BitmapFactory.decodeStream(is);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		client.close();
		return result;
	}
	
}
